package sgi.client;

import java.io.IOException;

import javax.swing.SwingUtilities;

import sgi.message.MessageApproveTariff;
import sgi.message.MessageChangeTariff;
import sgi.message.MessageRegister;
import sgi.message.MessageRetrieveTariff;

public class SGITariffController extends SGIController{

	
	private static SGITariffController tariffController;
	private static SGITariffUI tariffUI;
	
	public static SGITariffController create(){
		if(SGITariffController.tariffController == null){
			SGITariffController.tariffController = new SGITariffController();
			SGITariffController.tariffUI.setSgiTariffController(tariffController);
		}
		SGITariffController.tariffUI.setVisible(true);
		return SGITariffController.tariffController; 
	}	
	
	private SGITariffController (){
		SGITariffController.tariffUI = new SGITariffUI();
		this.requestTariff();
	}
	
	public static void terminate(){
		SGITariffController.tariffUI.setVisible(false);
		SGITariffController.tariffUI = null;
		SGITariffController.tariffController = null;
	}
	
	public static SGITariffUI getUI(){
		return SGITariffController.tariffUI;
	}
	
	public void changeTariff(float image,float timeLocation,float imagePack,float dataLayer){		
		MessageChangeTariff messageChangeTariff = new MessageChangeTariff(image, timeLocation, imagePack, dataLayer);
		try {
			SGIClientConnectionController.createConnection().handleMessageFromClientUI(messageChangeTariff);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	public void requestUpdate() {
		MessageChangeTariff messageChangeTariff = new MessageChangeTariff(-1,-1,-1,-1);
		messageChangeTariff.updatePendings();
		try {
			SGIClientConnectionController.createConnection().handleMessageFromClientUI(messageChangeTariff);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private void requestTariff(){
		MessageRetrieveTariff messageRetrieveTariff = new MessageRetrieveTariff(true);
		try {
			SGIClientConnectionController.createConnection().handleMessageFromClientUI(messageRetrieveTariff);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override	
	void handleMessage(final Object msg) {
		
		if(msg instanceof MessageRetrieveTariff)
		{
			SwingUtilities.invokeLater(new Runnable() {		
				@Override
				public void run() {
					MessageRetrieveTariff messageRetrieveTariff = (MessageRetrieveTariff) msg;
					SGITariffController.tariffUI.loadCurrentTariff(messageRetrieveTariff.getCurrentTariff());
					if(messageRetrieveTariff.hasPendings()){
						SGITariffController.tariffUI.loadPendingTariff(messageRetrieveTariff.getPendingTariff());
					}
				}
			});

		}
		else if(msg instanceof MessageApproveTariff)
		{
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					SGITariffController.tariffUI.updateOnlineApprove(((MessageApproveTariff)msg).isApprove());
				}
			});
		}		
	}
}
